package xcu.lxj.nettychat.user.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xcu.lxj.nettychat.common.domain.enums.NormalOrNoEnum;
import xcu.lxj.nettychat.common.domain.vo.request.CursorPageBaseReq;
import xcu.lxj.nettychat.common.domain.vo.response.CursorPageBaseResp;
import xcu.lxj.nettychat.common.utils.CursorUtils;
import xcu.lxj.nettychat.user.domain.entity.User;
import xcu.lxj.nettychat.user.domain.enums.ChatActiveStatusEnum;
import xcu.lxj.nettychat.user.mapper.UserMapper;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author XinJian Lei
 * @Date 2024/9/28 17:21
 * @Version 1.0
 */
@Service
public class UserDao extends ServiceImpl<UserMapper, User> {

    public User getByEmailAndPassword(String email , String password){
        return lambdaQuery()
                .eq(User::getEmail,email)
                .eq(User::getPassword,password)
                .one();
    }

    public void modifyName(Long uid, String name) {
        User update = new User();
        update.setId(uid);
        update.setName(name);
        updateById(update);
    }


    public User getByName(String name) {
        return lambdaQuery().eq(User::getName, name).one();
    }


    public List<User> getMemberList() {
        return lambdaQuery()
                .eq(User::getStatus, NormalOrNoEnum.NORMAL.getStatus())
                .orderByDesc(User::getLastOptTime)//最近活跃的1000个人，可以用lastOptTime字段，但是该字段没索引，updateTime可平替
                .last("limit 1000")//毕竟是大群聊，人数需要做个限制
                .select(User::getId, User::getName, User::getAvatar)
                .list();
    }


    public List<User> getFriendList(List<Long> uids) {
        return lambdaQuery()
                .in(User::getId, uids)
                .select(User::getId, User::getActiveStatus, User::getName, User::getAvatar)
                .list();

    }


    public Integer getOnlineCount() {
        return getOnlineCount(null);
    }


    public Integer getOnlineCount(List<Long> memberUidList) {
        return Math.toIntExact(lambdaQuery()
                .eq(User::getActiveStatus, ChatActiveStatusEnum.ONLINE.getStatus())
                .in(CollectionUtil.isNotEmpty(memberUidList), User::getId, memberUidList)
                .count());
    }

    public CursorPageBaseResp<User> getCursorPage(List<Long> memberUidList, CursorPageBaseReq request, ChatActiveStatusEnum online) {
        return CursorUtils.getCursorPageByMysql(this, request, wrapper -> {
            wrapper.eq(User::getActiveStatus, online.getStatus());//筛选上线或者离线的
            wrapper.in(CollectionUtils.isNotEmpty(memberUidList), User::getId, memberUidList);//普通群对uid列表做限制
        }, User::getLastOptTime);
    }


    public CursorPageBaseResp<User> getCursorPageByName(CursorPageBaseReq request,String name){
        return CursorUtils.getCursorPageByMysql(this,request,wrapper ->{
            wrapper.like(User::getName,name);
        },User::getName);

    }


    public Map<Long,User> getBatch(List<Long> uidList){

        Map<Long, User> collect = lambdaQuery()
                .in(User::getId, uidList)
                .select(User::getId, User::getActiveStatus, User::getName, User::getAvatar,User::getSex)
                .list().stream().collect(Collectors.toMap(User::getId, user -> user));

        return collect;
    }
}
